//
//  OrderInformationViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/12/7.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "OrderInformationViewModel.h"
#import "NoDataFootTableViewHeaderFooterView.h"
#import "OrderStoreTableViewHeaderFooterView.h"
#import "ContactServiceTableViewHeaderFooterView.h"
#import "OrderTopStepsTableViewCell.h"
#import "OrderExpressTableViewCell.h"
#import "OrderBuysInformationTableViewCell.h"
#import "OrderGoodsInforTableViewCell.h"
#import "OrderNumberMoneInforTableViewCell.h"
#import "OrderInforMoneyTableViewCell.h"
#import "OrderInforBottomView.h"
#import "OrderOperationView.h"
#import "BtnMoreAlertView.h"

@interface OrderInformationViewModel ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *selfTableView;
    OrderInforBottomView * bottomView;
    UIView *selfView;
}

@property (nonatomic, strong) OrderInfomationDetailModel *model;
@property (nonatomic, strong) BtnMoreAlertView *alerV;

@end

@implementation OrderInformationViewModel

 - (instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView
{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        tableView.height = tableView.height - kHeightProportion_In_iPhoneTriploid(54);
        tableView.mj_footer.hidden = YES;
        tableView.mj_header.hidden = YES;
        tableView.delegate = self;
        tableView.dataSource = self;
        selfTableView = tableView;
        [tableView registerClass:[NoDataFootTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"NoDataFootTableViewHeaderFooterView"];
        [tableView registerClass:[OrderStoreTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"OrderStoreTableViewHeaderFooterView"];
        [tableView registerClass:[ContactServiceTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"ContactServiceTableViewHeaderFooterView"];
        [superView addSubview:tableView];
        selfView = superView;
        //
        bottomView = [[OrderInforBottomView alloc] initWithFrame:CGRectMake(0, kY_With_View(tableView, 0), KScreenWidth, kHeightProportion_In_iPhoneTriploid(54)) inSuperView:superView];
        kWeakSelf(self);
        bottomView.Block_BottomAction = ^(GLGWButton * _Nonnull b_But) {
            [weakself bottomBtnClikWith:b_But];
        };
    }
    return self;
}

/*
 0, // 下载合同
 1, // 再次购买
 2, // 取消并撤回
 3, // 取消
 4, // 去支付
 5, // 确认收货
 6, // 提货单
 7  // 申请发票
 8  // 更多
 9  // 分享
 */
// 底部按钮点击事件
- (void)bottomBtnClikWith:(GLGWButton *)btn
{
    switch (btn.tag) {
        case 340: // 下载合同
            break;
        case 341: // 再次购买
            
            break;
        case 342: // 取消并撤回
            [self showAlertV:@"您的订单卖家正在确认，您确定\n要撤回取消订单？" strArr:@[@"撤回订单", @"取消"]];
            break;
        case 343: // 取消
            [self showAlertV:@"您正在取消订单，您确定\n要取消订单？" strArr:@[@"取消订单", @"暂不取消"]];
            break;
        case 344: // 去支付
            
            break;
        case 345: // 确认收货
            [self showAlertV:@"您的订单正在确认收货，您确定\n已经收到货了么？" strArr:@[@"确定收货", @"取消"]];
            break;
        case 346: // 提货单
            
            break;
        case 347: // 申请发票
            break;
        case 348: // 更多按钮
            [self showMoreAlertV:btn];
            break;
        default:
            break;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(bottomButAction:model:)]) {
        OrderInfomationDetailDataModel *dataModel = self.model.data;
        [self.delegate bottomButAction:btn model:dataModel];
    }
}

- (void)showAlertV:(NSString *)str strArr:(NSArray *)arr
{
    OrderOperationView *opeartionView = [[OrderOperationView alloc] initWithPopOrderOperation:arr theTitleStr:@"操作提示" theTosStr:str];
    [opeartionView show];
    opeartionView.Block_CaseAction = ^(NSInteger tag) {
        if (tag == 311) {
//            [self.delegate payTheOrder];
        }
    };
}

- (void)showMoreAlertV:(GLGWButton *)btn
{
    btn.selected = !btn.isSelected;

    CGRect frea = [btn convertRect:btn.bounds toView:selfView];
    CGFloat kH = kHeightProportion_In_iPhoneTriploid(61);
    CGFloat newY = frea.origin.y - kH + kHeightProportion_In_iPhoneTriploid(4);
    CGRect newF = CGRectMake(kWidthProportion_In_iPhoneTriploid(40), newY, kWidthProportion_In_iPhoneTriploid(66), kH);
    self.alerV.frame = newF;
    kWeakSelf(self);
    _alerV.BlockBtnClick = ^(GLGWButton *btn) {
        [weakself bottomBtnClikWith:btn];
    };
    if (btn.selected) {
        [self.alerV showV];
    } else {
        [self.alerV hiddenV];
    }
}

- (BtnMoreAlertView *)alerV
{
    if (!_alerV) {
        _alerV = [[BtnMoreAlertView alloc] initBtnMoreAlertView:selfView arr:@[@"取消订单", @"下载合同"] bgImgStr:@"bg_gengduo_shang"];
    }
    return _alerV;
}


#pragma mark -- UITableViewDelegate,UITableViewDataSource
 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger cellNum = 3;
    switch (section) {
        case 0:{
            if (self.model.data.info.orderStatus == 1 || self.model.data.info.orderStatus == 2) {
                cellNum = 2;
            }
        }
            break;
        case 1:{
            cellNum = 0;
            if (self.model) {
                cellNum = self.model.data.info.orderGoods.count;
            }
        }
            break;
        case 2:{
            cellNum = 2;
        }
            break;
        case 3: {
            cellNum = 0;
            if (self.model) {
                cellNum = 3;
            }
            if (self.model.data.info.orderStatus == 1 || self.model.data.info.orderStatus == 2) {
                cellNum = 0;
            }
        }
            break;
        case 4:{
            cellNum = 1;
        }
            break;
            
        default:
            break;
    }
    return cellNum;
}

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat cellHeight = kHeightProportion_In_iPhoneTriploid(44);
    switch (indexPath.section) {
        case 0:{
            if (indexPath.row == 2) {
                cellHeight = kHeightProportion_In_iPhoneTriploid(95);
            }else{
                cellHeight = kHeightProportion_In_iPhoneTriploid(85);
            }
        }
            break;
        case 1:{
            cellHeight = kHeightProportion_In_iPhoneTriploid(95);
        }
            break;
        case 4:{
            cellHeight = kHeightProportion_In_iPhoneTriploid(141);
        }
            break;
            
        default:
            break;
    }
    return cellHeight;
}

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BaseTableViewCell * cell;
    switch (indexPath.section) {
        case 0:{
            if (indexPath.row == 0) {
                OrderTopStepsTableViewCell * cell_steos = [OrderTopStepsTableViewCell cellWithTableView:tableView];
                if (self.model) {
                    OrderInfomationDetailDataModel *infoModel = self.model.data;
                    cell_steos.detailDataModel = infoModel;
                }
                [cell_steos setSepartionLineWith:lineModel_Half];
                cell = cell_steos;
            } else if (indexPath.row == 1) {
                OrderExpressTableViewCell *cell_express = [OrderExpressTableViewCell cellWithTableView:tableView];
                [cell_express setSepartionLineWith:lineModel_Half];
                if (self.model) {
                    cell_express.dataModel = self.model.data;
                }
                cell = cell_express;
            } else {
                OrderBuysInformationTableViewCell * cell_infor = [OrderBuysInformationTableViewCell cellWithTableView:tableView];
                if (self.model) {
                    cell_infor.dataModel = self.model.data;
                }
                cell = cell_infor;
            }
        }
            break;
        case 1:{
            OrderGoodsInforTableViewCell * cell_goods = [OrderGoodsInforTableViewCell cellWithTableView:tableView];
            [cell_goods setSepartionLineWith:lineModel_Half];
            if (self.model) {
                OrderInfomationDetailDataInfoModel *infoModel = self.model.data.info;
                [cell_goods refreshOrderGoodsInforTableViewCell:infoModel index:indexPath];
            }
            cell = cell_goods;
        }
            break;
        case 2:
        case 3:{
            OrderNumberMoneInforTableViewCell * cell_mone = [OrderNumberMoneInforTableViewCell cellWithTableView:tableView];
            [cell_mone setSepartionLineWith:lineModel_Half];
            if (self.model) {
                OrderInfomationDetailDataInfoModel *infoModel = self.model.data.info;
                cell_mone.detailModel = infoModel;
            }
            cell_mone.moneyPath = indexPath;
            cell = cell_mone;
        }
            break;
        case 4:{
            OrderInforMoneyTableViewCell * cell_money = [OrderInforMoneyTableViewCell cellWithTableView:tableView];
            if (self.model) {
                OrderInfomationDetailDataInfoModel *infoModel = self.model.data.info;
                [cell_money refreshOrderInforMoneyTableViewCell:infoModel index:indexPath];
            }
            cell = cell_money;
        }
            break;
            
        default:
            break;
    }
    return cell;
}

 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableViewHeaderFooterView * headerView;
    if (section == 1) {
        OrderStoreTableViewHeaderFooterView * headerView_Store = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"OrderStoreTableViewHeaderFooterView"];
        headerView = headerView_Store;
    } else {
        NoDataFootTableViewHeaderFooterView * headerView_noData = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"NoDataFootTableViewHeaderFooterView"];
        headerView = headerView_noData;
    }
    return headerView;
}

 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return kHeightProportion_In_iPhoneTriploid(28);
    } else {
        if (section == 3) {
            if (self.model.data.info.orderStatus == 1 || self.model.data.info.orderStatus == 2) {
                return 0.01;
            }
        }
        return kHeightProportion_In_iPhoneTriploid(10);
    }
}

 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UITableViewHeaderFooterView * footView;
    if (section == 1) {
        ContactServiceTableViewHeaderFooterView * footView_service = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"ContactServiceTableViewHeaderFooterView"];
        footView = footView_service;
    }else{
        NoDataFootTableViewHeaderFooterView * footView_noData = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"NoDataFootTableViewHeaderFooterView"];
        footView = footView_noData;
    }
    return footView;
}

 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    CGFloat footViewHeight = 0.01;
    switch (section) {
        case 0:
        case 4:{
            footViewHeight = kHeightProportion_In_iPhoneTriploid(10);
        }
            break;
        case 1:{
            footViewHeight = kHeightProportion_In_iPhoneTriploid(45);
        }
            break;
            
        default:
            break;
    }
    return footViewHeight;
}

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(expressDidSelectRowAtIndexPath:model:)]) {
        OrderInfomationDetailDataModel *dataModel = self.model.data;
        [self.delegate expressDidSelectRowAtIndexPath:indexPath model:dataModel];
    }
}

- (void)setOrderID:(NSString *)orderID
{
    _orderID = orderID;
    [self requestOrderDetail];
}

#pragma mark - 请求接口
- (void)requestOrderDetail
{
    NSDictionary *param = @{
                            @"orderId" : koString(self.orderID)
                            };
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kMyOrderListDetail] lodingView:nil lodingTextString:@"" isLoding:YES parameters:param success:^(id  _Nullable response) {
        self.model = [OrderInfomationDetailModel yy_modelWithJSON:response];
//        self.model.data.info.orderStatus = 3;
        self->bottomView.detailModel = self.model.data.info;
        [self->selfTableView reloadData];
    } failure:^(NSError * _Nullable error) {
    }];
}
@end
